﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "ApplicationManager.h"
#include "JournalUtils.h"
#include "IApplicationConfig.h"
#include "ISplashScreen.h"
#include "IApplication.h"
#include "IMainWindow.h"
#include "IApplicationEvent.h"
#include "IApplicationWindowEvent.h"
#include "GbmpApplicationEventHandler.h"
#include "GbmpMainWindowEventHandler.h"
#include "IDocumentManager.h"
// 用于指定插件加载的目录
#include "IAddinManager.h"

// 用于在云材质库界面显示 tooltip
#include "GgpCloudMaterialLibraryUiManifestConfig.h"
#include "GbmpFileSystem.h"
#include "FilePath.h"
#include "IHotKeyConfig.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"
using namespace gcmp;

int main(int argc, char *argv[])
{
    IApplication* pApp = ApplicationManager::Create(argc, argv);
    IDocumentManager::Get()->EnableDocAsyncDestruct();
    if (JournalUtils::IsInReplay() == false)
    {
        ISplashScreen* pSplashScreen = pApp->GetSplashScreen();
        pSplashScreen->SetImagePath(L":/images/startup.png");
        pSplashScreen->Show();
    }
    // 程序主窗口，main函数维护生命周期
    //OwnerPtr<IApplicationWindow> opMainWindow = IApplicationWindowFactory::Get()->CreateApplicationWindow();
    OwnerPtr<IApplicationWindow> opMainWindow = IMainWindow::Create();
    GbmpMainWindowEventHandler::InitUiSkin(opMainWindow.get());

    IApplicationConfig* pIAppConfig = pApp->GetConfig();
    if (pIAppConfig)
    {
        pIAppConfig->SetApplicationIconPath(L":/images/GbmpSysButton46.png");
        pIAppConfig->SetCategoryAndStyleConfigDirectory(L"gbmp_category_and_style_config");
        pIAppConfig->SetLineStyleSwitchesConfigDirectory(FileSystem::GetExeDirPath());
        pIAppConfig->SetUseInstanceMovablePoints(true);
    }

    IHotKeyConfig* pHotkeyConfig = IHotKeyConfig::Get();
    if (pHotkeyConfig)
    {
        pHotkeyConfig->SetHotKeyConfigFilePath(L"HotKeyConfig.xml");
    }

    IAddinManager* pAddinManager = pApp->GetAddinManager();
    if (pAddinManager)
    {
        pAddinManager->SetAddinConfigDirectory(L"gbmp_plugin_config");
    }

    OwnerPtr<GbmpApplicationEventHandler> pAppEventHandler = NEW_AS_OWNER_PTR(GbmpApplicationEventHandler);
    IApplicationEvent* pAppEvent = pApp->GetApplicationEvent();
    pAppEvent->Add(pAppEventHandler.get());

    OwnerPtr<GbmpMainWindowEventHandler> pMainWindowEventHandler = NEW_AS_OWNER_PTR(GbmpMainWindowEventHandler);
    IApplicationWindowEvent* pMainWindowEvent = IMainWindow::GetMainWindow()->GetMainWindowEvent();
    pMainWindowEvent->Add(pMainWindowEventHandler.get());

    for (int i = 0; i < argc; ++i)
    {
        GbmpString paramString = StringUtil::ToWString(argv[i]);
        if (paramString.find(".js", 0) > 0)
        {
            JournalUtils::SetIsInReplay(true);
            break;
        }
    }
    DebugModeManager::Singleton().SetRegistedDebugModeFromCommandLineArguments();

    int result = pApp->Execute();
    // 在程序销毁前，销毁主窗口
    StopExitProcessHook();
    opMainWindow.reset();
    ApplicationManager::Destroy();
    IDocumentManager::Get()->CleanDocAsyncDestructThread();
    return result;
}
